﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using KNS.Invoice.UI;
using InvoiceManagement.Controllers;
using DAL;
using BLL;
using DAL.Linq;

namespace InvoiceManagement
{
    public partial class frmDMKhachHang : KNSFormBase
    {
        #region "Khai báo biến"
        public frmDMKhachHang()
        {
            InitializeComponent();
        }

        public string strAction = "";
        #endregion
       
        #region "Các hàm load dữ liệu"
        public void GetDMKhachHang()
        {
            IList<DMKHACHHANG> lst = null;
            DM_KHACHHANGController KH_Controller = new DM_KHACHHANGController();
            lst = KH_Controller.GetAll_DMKhachHang();
            grdData.DataSource = lst;
        }
        #endregion

        #region "Các hàm sự kiện trên form"
        private void frmDMKhachHang_Load(object sender, EventArgs e)
        {
            strAction = "Load";
            SetControls();
            GetDMKhachHang();
            grdData_Click(sender, e);   
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            strAction = "AddNew";
            SetControls();
            ClearText();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            strAction = "Edit";
            SetControls();
            txtMaKhachHang.Enabled = false;
            txtTenKhachHang.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                bool bln = false;
                DM_KHACHHANGController KH_Controller = new DM_KHACHHANGController();                  
                if (CheckData() == true)
                {
                    if (strAction == "AddNew")
                    {
                        bln = KH_Controller.DM_KhachHang_Insert(txtMaKhachHang.Text.Trim().ToString(), txtTenKhachHang.Text.Trim().ToString(),
                            txtTenCongTy.Text.Trim().ToString(), txtDiaChi.Text.Trim().ToString(), txtDienThoai.Text.Trim().ToString(),
                            txtFax.Text.Trim().ToString(), txtWebsite.Text.Trim().ToString(), txtTaiKhoan.Text.Trim().ToString(),
                            txtMaSoThue.Text.Trim().ToString(), chkConSuDung.Checked, clsCommon.uID);
                    }
                    else
                    {
                        if (strAction == "Edit")
                        {
                            bln = KH_Controller.DM_KhachHang_Update(txtMaKhachHang.Text.Trim().ToString(), txtTenKhachHang.Text.Trim().ToString(),
                                txtTenCongTy.Text.Trim().ToString(), txtDiaChi.Text.Trim().ToString(), txtDienThoai.Text.Trim().ToString(),
                                txtFax.Text.Trim().ToString(), txtWebsite.Text.Trim().ToString(), txtTaiKhoan.Text.Trim().ToString(),
                                txtMaSoThue.Text.Trim().ToString(), chkConSuDung.Checked, clsCommon.uID, clsCommon.uID, DateTime.Now);
                        }
                    }

                    if (bln == true)
                    {
                        KNSMessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        strAction = "Load";
                        SetControls();
                        GetDMKhachHang();
                        grdData_Click(sender, e);                        
                    }
                    else
                    {
                        KNSMessageBox.Show("Cập nhật thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch { }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            strAction = "Load";
            SetControls();
            GetDMKhachHang();
            grdData_Click(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.RowCount > 0)
                {
                    if (KNSMessageBox.Show("Bạn có chắc muốn xoá?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        bool bln = false;
                        DM_KHACHHANGController KH_Controller = new DM_KHACHHANGController();
                        bln = KH_Controller.DM_KhachHang_Delete(txtMaKhachHang.Text);
                        if (bln == true)
                        {
                            KNSMessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            strAction = "Load";
                            SetControls();
                            GetDMKhachHang();
                            grdData_Click(sender, e);
                        }
                        else
                        {
                            KNSMessageBox.Show("Cập nhật thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                else
                {
                    KNSMessageBox.Show("Không có dữ liệu để xoá", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            catch { }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grdData_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0)
            {
                int[] arrSelected = gridView1.GetSelectedRows();
                if (arrSelected != null && arrSelected.Length > 0)
                {
                    int selectedIndex = arrSelected[0];
                    if (selectedIndex >= 0)
                    {
                        Object a = gridView1.GetRow(selectedIndex);
                        if (a != null)
                        {
                            txtMaKhachHang.Text = (a as DMKHACHHANG).MaKhachHang.ToString();
                            txtTenKhachHang.Text = (a as DMKHACHHANG).TenKhachHang.ToString();
                            txtTenCongTy.Text = (a as DMKHACHHANG).TenCongTy.ToString();
                            txtDiaChi.Text = (a as DMKHACHHANG).DiaChi.ToString();
                            txtDienThoai.Text = (a as DMKHACHHANG).DienThoai.ToString();
                            txtFax.Text = (a as DMKHACHHANG).Fax.ToString();
                            txtWebsite.Text = (a as DMKHACHHANG).WebSite.ToString();
                            txtTaiKhoan.Text = (a as DMKHACHHANG).TaiKhoan.ToString();
                            txtMaSoThue.Text = (a as DMKHACHHANG).MaSoThue.ToString();
                            chkConSuDung.Checked = (bool)(a as DMKHACHHANG).IsUse;
                        }
                    }
                }
                //txtUserCode.Text = strUsercode;
                //txtPassword.Text = strUserPass;
                //txtUserName.Text = strUserName;
                //chkIsUse.Checked = blnIsUse;
            }
        }
        #endregion
       
        #region "Các hàm xử lý khác"
        public void SetControls()
        {
            if (strAction == "Load")
            {
                txtMaKhachHang.Enabled = false;
                txtTenKhachHang.Enabled = false;
                txtTenCongTy.Enabled = false;
                txtDiaChi.Enabled = false;
                txtDienThoai.Enabled = false;
                txtFax.Enabled = false;
                txtWebsite.Enabled = false;
                txtTaiKhoan.Enabled = false;
                txtMaSoThue.Enabled = false;
                chkConSuDung.Enabled = false;

                grdData.Enabled = true;

                btnThemMoi.Enabled = true;
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                btnHuy.Enabled = false;
                //btnTimKiem.Enabled = true;
                btnDelete.Enabled = true;
                btnThoat.Enabled = true;
            }
            else
            {
                if (strAction == "AddNew" || strAction == "Edit")
                {
                    txtMaKhachHang.Enabled = true;
                    txtTenKhachHang.Enabled = true;
                    txtTenCongTy.Enabled = true;
                    txtDiaChi.Enabled = true;
                    txtDienThoai.Enabled = true;
                    txtFax.Enabled = true;
                    txtWebsite.Enabled = true;
                    txtTaiKhoan.Enabled = true;
                    txtMaSoThue.Enabled = true;
                    chkConSuDung.Enabled = true;

                    grdData.Enabled = false;

                    btnThemMoi.Enabled = false;
                    btnSua.Enabled = false;
                    btnLuu.Enabled = true;
                    btnHuy.Enabled = true;
                    //btnTimKiem.Enabled = false;
                    btnDelete.Enabled = false;
                    btnThoat.Enabled = true;
                }
            }
        }

        public void ClearText()
        {
            txtMaKhachHang.Text = "";
            txtTenKhachHang.Text = "";
            txtTenCongTy.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtFax.Text = "";
            txtWebsite.Text = "";
            txtTaiKhoan.Text = "";
            txtMaSoThue.Text = "";
            chkConSuDung.Checked = false;
            txtMaKhachHang.Focus();
        }

        public bool CheckData()
        {
            try
            {
                if (txtMaKhachHang.Text == string.Empty || txtMaKhachHang.Text == "")
                {
                    KNSMessageBox.Show("Chưa nhập mã khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    txtMaKhachHang.Focus();
                    return false;
                }

                if (txtTenKhachHang.Text == string.Empty || txtTenKhachHang.Text == "")
                {
                    KNSMessageBox.Show("Chưa nhập tên khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    txtTenKhachHang.Focus();
                    return false;
                }
                return true;
            }
            catch { return false; }
        }

        #endregion               
        
    }
}